package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    @Autowired
    private AdSensitiveMapper adSensitiveMapper;

    //敏感词查询
    @Override
    public ResponseResult list(SensitiveDTO dto) {
        //创建wrapper
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        //非空判断
        if (dto.getName() == null) {
            //没有内容,查询全部
            List<AdSensitive> adSensitives = adSensitiveMapper.selectList(wrapper);
            //封装数据返回结果
            ResponseResult<Object> responseResult = new ResponseResult<>();
            responseResult.setData(adSensitives);
            return responseResult;
        }
        //有内容,根据name条件查询
        wrapper.like(AdSensitive::getSensitives, dto.getName());
        List<AdSensitive> adSensitives = adSensitiveMapper.selectList(wrapper);
        //封装数据返回结果
        ResponseResult<Object> responseResult = new ResponseResult<>();
        responseResult.setData(adSensitives);
        return responseResult;
    }

    //敏感词添加
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //非空判断
        if (adSensitive == null) {
            //没有数据,提示错误
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //设置查询条件
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        if (adSensitive.getSensitives() != null) {
            wrapper.eq(AdSensitive::getSensitives, adSensitive.getSensitives());
        }
        //根据敏感词条件查询
        List<AdSensitive> adSensitives = adSensitiveMapper.selectList(wrapper);
        //判断是否存在
        if (adSensitives != null && adSensitives.size() != 0) {
            //该敏感词已经存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //敏感词不存在,可以插入数据
        adSensitive.setCreatedTime(new Date());
        adSensitiveMapper.insert(adSensitive);
        //响应成功结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //敏感词修改
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //非空判断
        if (adSensitive == null) {
            //没有数据,提示错误
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //设置查询条件:id
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        if (adSensitive.getId() != null) {
            wrapper.eq(AdSensitive::getId, adSensitive.getId());
        }
        //根据Id查询
        AdSensitive adSensitive2 = adSensitiveMapper.selectById(adSensitive.getId());
        if (adSensitive2 == null) {
            //根据id未查询到数据,提示错误,不存在此id
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //有数据,可以修改,判断修改后的敏感词是否存在
        LambdaQueryWrapper<AdSensitive> wrapper2 = Wrappers.lambdaQuery();
        wrapper2.eq(AdSensitive::getSensitives, adSensitive.getSensitives());
        List<AdSensitive> adSensitives = adSensitiveMapper.selectList(wrapper2);
        //判断是否存在
        if (adSensitives != null && adSensitives.size() != 0) {
            //该敏感词存在,不能修改
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //敏感词不存在,可以修改数据
        LambdaUpdateWrapper<AdSensitive> wrapper3 = Wrappers.lambdaUpdate();
        wrapper3.eq(AdSensitive::getId, adSensitive.getId());
        adSensitiveMapper.update(adSensitive, wrapper3);
        //响应成功结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //敏感词删除
    @Override
    public ResponseResult delete(Integer id) {
        //非空判断
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id查询
        AdSensitive adSensitive = adSensitiveMapper.selectById(id);
        //判断
        if (adSensitive == null) {
            //不存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //存在,删除该敏感词
        adSensitiveMapper.deleteById(id);
        //响应结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //查询所有敏感词,只要单独的敏感词本身
    @Override
    public ResponseResult selectAllSensitives() {

         List<String> sensitives = adSensitiveMapper.selectAllSensitives();

         return ResponseResult.okResult(sensitives);
    }
}
